package util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;


public class FileHandler {
	private static FileHandler instance;
	private static final String RESULTSFILENAME = "results.txt";
	private static final String FOLDER = "output" + File.separator;
	
	private FileHandler() {
	}
	
	public static FileHandler getInstance() {
		if(instance == null) {
			instance = new FileHandler();
		}
		return instance;
	}
	
	public void write(String s, String file) {
		FileWriter fw;		
		try {
			fw = new FileWriter(FOLDER + file, true);
			BufferedWriter out = new BufferedWriter(fw);
			out.write(s);	
			out.close();
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteFile(String fileName) {
		File f = new File(FOLDER + fileName);
		f.delete();
	}
	
	public void deleteGlobalFile() {
		deleteFile(RESULTSFILENAME);
	}
	
	public void addToGlobalFile(String s) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(FOLDER + RESULTSFILENAME, true));
			out.write(s);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}
